package com.sky.haikangdemo.service;

import com.sky.haikangdemo.utils.HikVisionSDKInitUtil;
import com.sky.haikangdemo.config.PlateConfig;
import com.sky.haikangdemo.entity.ParkAccessTemporary;
import com.sky.haikangdemo.entity.PlateInfo;
import com.sky.haikangdemo.mapper.EnterMapper;
import com.sky.haikangdemo.mapper.GateMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Service("enterService")
@Slf4j
public class EnterService {

    @Autowired
    private EnterMapper enterMapper;
    @Autowired
    private GateMapper gateMapper;

    @Transactional(rollbackFor = Exception.class)
    public void plateEnter(PlateInfo plateInfo, String content) {
        String key = PlateConfig.deviceIp + PlateConfig.devicePort + PlateConfig.username + PlateConfig.password;
        try {
            log.info("车牌号:{}", plateInfo.getPlate());
            ParkAccessTemporary parkAccessTemporary = new ParkAccessTemporary();
            parkAccessTemporary.setOrderId(UUID.randomUUID().toString().substring(0, 10));
            parkAccessTemporary.setIntoTime(new Date());
            parkAccessTemporary.setIntoDevice(plateInfo.getDeviceId());
            parkAccessTemporary.setCarType(4);
            parkAccessTemporary.setPlateType(plateInfo.getPlateType());
            parkAccessTemporary.setPlate(plateInfo.getPlate());

            enterMapper.insertSelective(parkAccessTemporary);


            /**
             * led播报
             */
            HikVisionSDKInitUtil.ledBroadcast(key, "临时车" + plateInfo.getPlate() + " 欢迎光临", plateInfo.getPlate());

            /**
             * 语音播报
             */
            HikVisionSDKInitUtil.voiceBroadcast(key, "临时车" + plateInfo.getPlate() + "欢迎光临，" + content);

            /**
             * 远程抬杆
             */
            HikVisionSDKInitUtil.remoteControl(key, PlateConfig.POLE_OPEN);
        } catch (Exception e) {
            HikVisionSDKInitUtil.voiceBroadcast(key, "该车已入场，请勿重复抬杆");
            throw new RuntimeException("远程抬杆失败");
        }
    }
}
